﻿using PBO.Data.Domain;
using PBO.Data.SQLite;
using System;
using System.Collections.Generic;
using System.Linq;

namespace PBO.Data
{
    [Serializable]
    public class PokemonData : GameElement
    {

        public PokemonData()
        {
            BaseStats = new BaseStats();
        }

        #region props

        public BaseStats BaseStats { get; internal set; }
        public int BeforeEvolution { get; internal set; }
        public EggGroup EggGroup1 { get; internal set; }
        public EggGroup EggGroup2 { get; internal set; }
        public int Forme { get; internal set; }
        public PokemonGenderRestriction GenderRestriction { get; internal set; }
        public string NameBase { get; internal set; }
        public int Number { get; internal set; }
        public Trait Trait1 { get; internal set; }
        public Trait Trait2 { get; internal set; }
        public BattleType Type1 { get; internal set; }
        public BattleType Type2 { get; internal set; }
        public double Weight { get; internal set; }
        public int UsageCount { get; internal set; }

        public Item ItemRestriction
        {
            get
            {
                return this.Number == 487 && this.Forme == 1 ? Item.GriseousOrb : Item.None;
            }
        }

        #endregion

        #region EggGroup

        public IEnumerable<EggGroup> EggGroups
        {
            get
            {
                yield return EggGroup1;
                if (EggGroup2 != EggGroup.无) yield return EggGroup2;
            }
        }

        #endregion

        #region Gender
        private static readonly IEnumerable<PokemonGender> NONE = new[] { PokemonGender.None };
        private static readonly IEnumerable<PokemonGender> MALE = new[] { PokemonGender.Male };
        private static readonly IEnumerable<PokemonGender> FEMALE = new[] { PokemonGender.Female };
        private static readonly IEnumerable<PokemonGender> BOTH = new[] { PokemonGender.Male, PokemonGender.Female };

        public IEnumerable<PokemonGender> Genders
        {
            get
            {
                switch (this.GenderRestriction)
                {
                    case PokemonGenderRestriction.MaleOnly:
                        return MALE;
                    case PokemonGenderRestriction.FemaleOnly:
                        return FEMALE;
                    case PokemonGenderRestriction.NoGender:
                        return NONE;
                    default:
                        return BOTH;
                }
            }
        }
        #endregion

        #region Ability

        public IEnumerable<Trait> Traits
        {
            get
            {
                yield return this.Trait1;
                if (this.Trait2 != Trait.None) yield return this.Trait2;
            }
        }

        public IEnumerable<Ability> Abilities
        {
            get
            {
                foreach (Trait t in this.Traits)
                {
                    yield return GameService.GetAbility(t);
                }
            }
        }

        public Trait GetTrait(int selectedIndex)
        {
            if (selectedIndex <= 1) return Traits.First();
            var arr = Traits.ToArray();
            selectedIndex = Math.Min(arr.Length, selectedIndex);
            return arr[selectedIndex - 1];
        }

        public byte GetSelectedTrait(Trait trait)
        {
            return (this.Trait2 != Trait.None && trait == this.Trait2) ? (byte)2 : (byte)1;
        }

        #endregion

        #region Image

        public long FrontImage { get; internal set; }
        public long FrontImageF { get; internal set; }
        public long BackImage { get; internal set; }
        public long BackImageF { get; internal set; }
        public long Icon { get; internal set; }
        public long Frame { get; internal set; }
        public long FrameF { get; internal set; }

        #endregion

    }
}
